package ciprian.google;

/**
 * @author Ciprian Ursu
 */

import ciprian.google.R;
import ciprian.google.location_mgr.MapPoint;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.view.View;

public class AddPOIActivity extends Activity implements OnClickListener {

    protected ImageView cancelButton;
    protected ImageView applyButton;
    protected TextView selectedCoordinatesTextView;
    protected RadioGroup poiTypeRadioGroup;
    protected EditText poiNameEditText;

    protected double coordinates[];

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_poi);

        cancelButton = (ImageView) findViewById(R.id.CancelAddPOIImageView);
        applyButton = (ImageView) findViewById(R.id.ApplyAddPOIImageView);
        poiTypeRadioGroup = (RadioGroup) findViewById(R.id.POIRadioGroup);
        poiNameEditText = (EditText) findViewById(R.id.NamePOIEditText);

        selectedCoordinatesTextView = (TextView) findViewById(R.id.CoordinatesPOITextView);
    }

    @Override
    public void onResume() {
        super.onResume();

        // get the coordinates sent by MapViewActivity.
        Intent intent = this.getIntent();
        coordinates = intent.getDoubleArrayExtra(Constants.POI_COORDINATES);
        selectedCoordinatesTextView.setText(getText(R.string.latitude).toString() + coordinates[0]
                + ", " + getText(R.string.longitude).toString() + coordinates[1]);

        poiTypeRadioGroup.requestFocus();

        cancelButton.setOnClickListener(this);
        applyButton.setOnClickListener(this);
    }

    /**
     * Exits this activity.
     */
    protected void exitActivity() {
        this.finish();
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.CancelAddPOIImageView:
            this.setResult(RESULT_CANCELED);
            exitActivity();
            break;
            
        case R.id.ApplyAddPOIImageView:
            applyChanges();
            break;
        }
    }
    
    private void applyChanges() {
        Intent intent = AddPOIActivity.this.getIntent();
        intent.putExtra(Constants.POI_COORDINATES, coordinates);

        int poiType = MapPoint.POI_NICE_TO_VIEW;
        String poiName = poiNameEditText.getText().toString();

        switch (poiTypeRadioGroup.getCheckedRadioButtonId()) {
        case R.id.HutRadioButton:
            poiType = MapPoint.POI_HUT;
            break;

        case R.id.BelleviewRadioButton:
            poiType = MapPoint.POI_NICE_TO_VIEW;
            break;

        case R.id.CampingRadioButton:
            poiType = MapPoint.POI_CAMPING;
            break;

        case R.id.PeakPOIRadioButton:
            poiType = MapPoint.POI_PEAK;
            break;

        case R.id.ShelterRadioButton:
            poiType = MapPoint.POI_SHELTER;
            break;
        }
        
        intent.putExtra(Constants.POI_TYPE, poiType);
        intent.putExtra(Constants.POI_NAME, poiName);

        AddPOIActivity.this.setResult(RESULT_OK, intent);
        exitActivity();
    }
}
